package cn.neusoft.controller;


import cn.neusoft.po.Category;
import cn.neusoft.po.Department;
import cn.neusoft.service.ICategoryService;
import cn.neusoft.vo.ResponseObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author hxj
 * @since 2023-12-21
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    ICategoryService service;

    @GetMapping("/tree")
    public ResponseObj tree(){
        List<Category> tree = service.tree();
        return ResponseObj.SUCCESS(tree);
    }

    @PostMapping
    public ResponseObj saveOrUpdate(@RequestBody Category category){
        boolean b = service.saveOrUpdate(category);
        return b?ResponseObj.SUCCESS(category):ResponseObj.ERROR(60001,"保存商品类别信息失败");
    }

    @DeleteMapping("/{id}")
    public ResponseObj delete(@PathVariable("id") Integer id){
        boolean b = service.removeById(id);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(60002,"删除类别信息失败");
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Integer id){
        Category category = service.getById(id);
        return ResponseObj.SUCCESS(category);
    }

    @PostMapping("/changeState")
    public ResponseObj changeState(@RequestBody Category category){
        boolean b = service.changeState(category);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(60002,"更新类别状态失败");
    }
}
